import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:image_picker/image_picker.dart';
import 'package:motion_toast/motion_toast.dart';
import 'package:motion_toast/resources/arrays.dart';
import 'package:nylo_framework/nylo_framework.dart';

import '../../bootstrap/helpers.dart';
import '../../resources/widgets/logo_widget.dart';
import 'controller.dart';
import 'package:flutter/widgets.dart';

class DeliverController extends Controller {
  TextEditingController content = TextEditingController();
  TextEditingController wxNumber = TextEditingController();
  TextEditingController qqNumber = TextEditingController();
  List<String> imageList = [];

  construct(BuildContext context) {
    imageList = [];
    content.text = "";
    wxNumber.text = "";
    qqNumber.text = "";
    super.construct(context);
  }

  openCamera(context, refreshPage) async {
    Navigator.pop(context); // 回到主界面
    XFile? pickedFile = await ImagePicker().pickImage(
        // 拍照获取图片
        source: ImageSource.camera,
        // 减小图片的文件大小并提高加载和显示的效率
        maxWidth: 400,
        maxHeight: 400);
    if (null != pickedFile) {
      imageList.add(pickedFile.path);
      refreshPage();
    }
  }

  openGallery(context, refreshPage) async {
    Navigator.pop(context); // 回到主界面
    List<XFile> files = await ImagePicker().pickMultiImage(
        // 减小图片的文件大小并提高加载和显示的效率
        maxHeight: 400,
        maxWidth: 400);
    if (!files.isEmpty) {
      if (imageList.length + files.length > 9) {
        files = files.sublist(0, 9 - imageList.length);
      }
      imageList.addAll(files.map((e) => e.path));
      refreshPage();
    }
  }

  showImagePickerDialog(context, refreshPage) {
    // 关闭键盘
    FocusScope.of(context).unfocus();
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return SimpleDialog(
          // title: Text(''),
          children: [
            SimpleDialogOption(
              onPressed: () => openCamera(context, refreshPage),
              child: Text('拍照'),
            ),
            SimpleDialogOption(
              onPressed: () => openGallery(context, refreshPage),
              child: Text('相册选择'),
            ),
          ],
        );
      },
    );
  }

  removeImage(int index, void Function({Function()? setState}) refreshPage) {
    imageList.removeAt(index);
    refreshPage();
  }

  void publicContent(
      BuildContext context, void Function({Function()? setState}) refreshPage) {
    // 关闭键盘
    FocusScope.of(context).unfocus();
    NyLogger.debug("content ${content.text}");
    NyLogger.debug("wx ${wxNumber.text}");
    NyLogger.debug("qq ${qqNumber.text}");
    NyLogger.debug("相册 ${imageList.toString()}");
    // 校验
  }

  Future<void> backPop(BuildContext context,
      void Function({Function()? setState}) refreshPage) async {
    if (content.text.isEmpty) {
      Navigator.of(context).pop();
      return;
    }
    // 关闭键盘
    FocusScope.of(context).unfocus();
    bool? quit = await sureQuit(context);
    if (quit!) {
      Navigator.of(context).pop();
    }
  }

  Future<bool?> sureQuit(context) async {
    return await showDialog<bool>(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text(
            '退出编辑',
            style: TextStyle(fontSize: 30.sp, fontWeight: FontWeight.bold),
          ),
          content: Text(
            '当前修改暂未提交，是否确认退出？',
            style: TextStyle(fontSize: 30.sp),
          ),
          actions: [
            ElevatedButton(
              style: ButtonStyle(
                backgroundColor: MaterialStateProperty.all<Color>(
                    ThemeColor.get(context)
                        .primaryBackgroundLight
                        .withOpacity(1)),
              ),
              onPressed: () => Navigator.of(context).pop(false),
              child: Text('取消'),
            ),
            ElevatedButton(
              style: ButtonStyle(
                backgroundColor: MaterialStateProperty.all<Color>(
                    ThemeColor.get(context)
                        .primaryBackgroundDeep
                        .withOpacity(1)),
              ),
              onPressed: () => Navigator.of(context).pop(true),
              child: Text('确认'),
            ),
          ],
        );
      },
    );
  }

  Future<bool> keyBack(BuildContext context,
      void Function({Function()? setState}) refreshPage) async {
    if (content.text.isEmpty) return true;
    bool? quit = await sureQuit(context);
    return quit!;
  }
}
